package cn.edu.eight.food.controller;


import cn.edu.eight.food.model.Foods;
import cn.edu.eight.food.model.OfComment;
import cn.edu.eight.food.model.OfFood;
import cn.edu.eight.food.service.ofCommentService;
import cn.edu.eight.food.service.ofFoodService;
import cn.edu.eight.food.util.Result;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/food")
public class ofFoodController {
    @Autowired
    ofFoodService ofFoodService;
    @Autowired
    ofCommentService ofCommentService;

    @RequestMapping("/foodMsg")
    @ResponseBody
    public Result list(String foodName, String hot){
        Result<Foods> foodsResult = new Result<>();
        List<Foods> foodsList = ofFoodService.foods(Integer.parseInt(hot),foodName);
        if (foodsList != null && foodsList.size() > 0 ){
            foodsResult.setCode("0");
            foodsResult.setMsg("请求成功");
            foodsResult.setCount(foodsList.size());
            foodsResult.setData(foodsList);
        }else {
            foodsResult.setCode("500");
            foodsResult.setMsg("没有数据！");
        }
        return foodsResult;
    }

    //修改食物的状态
    @RequestMapping("/save")
    @ResponseBody
    public Result save(@RequestParam(required = false) String action,
                       @RequestBody OfFood ofFood){
        Result result=new Result();
        if (action == null || action.equals("")){
            ofFoodService.updateFood(ofFood);
            result.setCode("200");
            return result;
        }
        if (action.equals("disable")){
            ofFood.setOfFoodState(2);
            ofFood.setOfFoodHot(1);
            ofFoodService.updateFood(ofFood);
            result.setCode("200");
            return result;
        }else if (action.equals("enable")){
            ofFood.setOfFoodState(1);
            ofFoodService.updateFood(ofFood);
            result.setCode("200");
            return result;
        }else if (action.equals("cancel")){
            ofFood.setOfFoodHot(1);
            ofFoodService.updateFood(ofFood);
            result.setCode("200");
            return result;
        }else if (action.equals("add")){
            ofFood.setOfFoodHot(2);
            ofFoodService.updateFood(ofFood);
            result.setCode("200");
            return result;
        }
        result.setCode("500");
        return result;
    }

    //添加食物
    @RequestMapping("/addFood")
    @ResponseBody
    public Result addFood(@RequestBody OfFood ofFood){
        Result result=new Result();
        ofFood.setOfFoodState(1);
        ofFood.setOfFoodHot(1);
        ofFoodService.insert(ofFood);
        result.setCode("200");
        return result;
    }

    //删除食物
    @RequestMapping("/del")
    @ResponseBody
    public Result delUser(@RequestBody String[] ids){
        Result result = new Result();
        for (int i=0;i<ids.length;i++){
            ofFoodService.deleteFood(ids[i]);
        }
        result.setCode("200");
        return result;
    }

    //食品展示
    @RequestMapping("/foodList")
    @ResponseBody
    public Result foodList(@RequestBody String hots){
        JSONObject jsonObject = JSONObject.parseObject(hots);
        List<OfFood> foodsList = ofFoodService.foodList(Integer.parseInt(jsonObject.getString("hots")));
        Result<OfFood> foodsResult = new Result<>();
        if (foodsList != null && foodsList.size() > 0 ){
            foodsResult.setCode("200");
            foodsResult.setMsg("请求成功");
            foodsResult.setCount(foodsList.size());
            foodsResult.setData(foodsList);
        }else {
            foodsResult.setCode("500");
            foodsResult.setMsg("没有数据！");
        }
        return foodsResult;
    }

    @RequestMapping("/foodDetail")
    private String foodDetail(@RequestParam String foodId, HttpServletRequest request){
        OfFood ofFood=ofFoodService.findByIdFood(foodId);
        request.setAttribute("ofFood",ofFood);
        List<OfComment> commentList=ofCommentService.findByFoodId(foodId);
        request.setAttribute("commentList",commentList);
        return "detail";
    }
}
